﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Common;

namespace InfiniDrive
{
    class VirtualFSAgent
    {
        #region Private Fields
        private ITree<VirtualFSInfo> vFS;
        private ITree<VirtualFSInfo> vFSfromXML;

        #endregion

        #region Public Properties
        /// <summary>
        /// The name of the file
        /// </summary>
        public INode<VirtualFSInfo> VFS
        {
            //get;
            //set;
            get { return vFS.Root; }
            // set { fileName = value; }
        }
        #endregion

        #region CTOR
        public VirtualFSAgent()
        {
            // Add dataComparer 
            vFS = NodeTree<VirtualFSInfo>.NewTree();
            VirtualFSInfo root = new VirtualFSInfo();
            root.FileName = "InfiniDrive";
            root.IsFolder = true;
            INode<VirtualFSInfo> top = vFS.AddChild(root);
            PopulateTree("C:\\Documents and Settings\\gjacob\\My Documents\\IsoTherm", top);
            CreateXML();
            GetFromXML();
        }


        #endregion

        #region Methods
        private void PopulateTree(string path, INode<VirtualFSInfo> node)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            FileSystemInfo[] infos = dir.GetFileSystemInfos();
            // Check the FSInfo parameter.
            if (infos == null)
            {
                throw new ArgumentNullException("infos");
            }

            // Iterate through each item.
            foreach (FileSystemInfo i in infos)
            {
                // Check to see if this is a DirectoryInfo object.
                if (i is DirectoryInfo)
                {
                    // Cast the object to a DirectoryInfo object.
                    DirectoryInfo d = (DirectoryInfo) i;
                    node.AddChild(new VirtualFSInfo(d));

                    // Iterate through all sub-directories.
                    PopulateTree(d.FullName, node.LastChild);
                }
                // Check to see if this is a FileInfo object.
                else if (i is FileInfo)
                {
                    // Cast the object to a FileInfo object.
                    FileInfo f = (FileInfo) i;
                    node.AddChild(new VirtualFSInfo(f));

                }
            }
        }
        
        public void CreateXML()
        {
            Stream strm = File.Open("C:\\Documents and Settings\\gjacob\\My Documents\\IsoTherm\\vFSinfo.xml", FileMode.Create);
            //StringWriter strm = new StringWriter();
            vFS.XmlSerialize(strm);
            //XmlSerializer xml = new XmlSerializer(typeof(INode<VirtualFSInfo>));
            //xml.Serialize(strm, this);
            strm.Close();
        }

        public void GetFromXML()
        {
            Stream strm = File.Open("C:\\Documents and Settings\\gjacob\\My Documents\\IsoTherm\\vFSinfo.xml", FileMode.Open, FileAccess.Read);
            vFSfromXML = NodeTree<VirtualFSInfo>.XmlDeserialize(strm);
            strm.Close();
        }

        #endregion

        #region Overrides

        #endregion
    }
}
